package com.jacars.biz.user.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jacars.biz.user.dao.service.JsptRoleService;
import com.jacars.common.annotation.LoginUser;
import com.jacars.common.api.PageResult;
import com.jacars.common.api.Result;
import com.jacars.common.model.JsptRole;
import com.jacars.common.model.JsptUser;
import com.jacars.db.support.Condition;
import com.jacars.db.support.Query;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author 作者 owen E-mail: 624191343@qq.com
 * 角色管理
 */
@Slf4j
@RestController
@RequestMapping(value = "/roles")
@AllArgsConstructor
@Api(tags = "角色模块api")
public class RoleController {

    @NotNull
    private final JsptRoleService jsptRoleService;

    /**
     * 后台管理查询角色
     *
     * @param jsptRole
     * @return
     */
    @ApiOperation(value = "后台管理查询角色")
    @GetMapping("/rolePage")
    public PageResult<JsptRole> rolePage(JsptRole jsptRole, Query query, @LoginUser JsptUser jsptUser) {

        IPage<JsptRole> page = null;
        //如果是全局管理员，可以查看所有的 如果是其他用户，只能查询本单位下的角色
        if (jsptUser.getId().equals(1L)) {
            page = Condition.getPage(query);
            List<JsptRole> all = jsptRoleService.findAll();
            Integer current = query.getCurrent();
            Integer size = query.getSize();
            int start = (current - 1) * size;
            int end = (current) * size;
            if (end > all.size()) end = all.size();
            page.setRecords(all.subList(start,end));
            page.setTotal(all.size());
        } else {
            page = jsptRoleService.page(Condition.getPage(query), Condition.getQueryWrapper(jsptRole));
        }

        return PageResult.succeed(page.getRecords(), page.getTotal());
    }

    /**
     * 用户管理查询所有角色
     *
     * @return
     */
    @ApiOperation(value = "后台管理查询角色")
    @GetMapping("/roleList")
    public Result<List<JsptRole>> roleList(@LoginUser(isFull = false) JsptUser jsptUser) {
        List<JsptRole> result = null;
        //如果是全局管理员，可以查看所有的 如果是其他用户，只能查询本单位下的角色
        if (jsptUser.getId().equals(1L)) {
            result = jsptRoleService.findAll();
        } else {
            result = jsptRoleService.list();
        }
        return Result.succeed(result);
    }

    /**
     * 角色新增或者更新
     *
     * @param jsptRole
     * @return
     */
    @PostMapping("/create")
    public Result create(@RequestBody JsptRole jsptRole) throws Exception {
        if (jsptRole.getTenantId().equals("")) {
            jsptRole.setTenantId(null);
        }
        jsptRoleService.save(jsptRole);
        return Result.succeed();
    }

    /**
     * 角色新增或者更新
     *
     * @param jsptRole
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody JsptRole jsptRole) throws Exception {
        if (jsptRole.getTenantId().equals("")) {
            jsptRole.setTenantId(null);
        }
        jsptRoleService.updateById(jsptRole);
        return Result.succeed();
    }


    /**
     * 后台管理删除角色
     * delete /role/1
     *
     * @param id
     */
    @ApiOperation(value = "后台管理删除角色")
    @PostMapping("/remove/{id}")
    public Result deleteRole(@PathVariable Long id) {
        try {
            if (id == 1L) {
                return Result.failed("管理员不可以删除");
            }
            jsptRoleService.deleteRole(id);
            return Result.succeed("操作成功");
        } catch (Exception e) {
            log.error("role-deleteRole-error", e);
            return Result.failed("操作失败");
        }
    }
}
